/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.basic.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.eelpo.cmmp.basic.dao.ISystemConfigManageDao;
import com.eelpo.cmmp.basic.dao.entity.TSystemConfig;
import com.eelpo.cmmp.basic.dao.vo.SystemConfig;
import com.eelpo.cmmp.basic.services.ISystemConfigManageService;
import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.common.util.BlankUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * 
 * 系统配置服务管理具体实现
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Service
public class SystemConfigManageService implements ISystemConfigManageService {

	@Resource
	private ISystemConfigManageDao systemConfigManageDao;

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.basic.services.ISystemConfigService#getSystemConfigById
	 *      (java.lang.String)
	 */
	@Override
	public SystemConfig getSystemConfigById(String id) throws ServiceException {

		TSystemConfig tconfig = systemConfigManageDao.getSystemConfigById(id);

		if (BlankUtils.isBlank(tconfig)) {
			throw new ServiceException("查询的系统配置不存在！");
		}

		return po2vo(tconfig);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.basic.services.ISystemConfigService#getSystemConfigByKey
	 *      (java.lang.String)
	 */
	@Override
	public SystemConfig getSystemConfigByKey(String key) throws ServiceException {

		TSystemConfig tconfig = systemConfigManageDao.getSystemConfigByKey(key);

		if (BlankUtils.isBlank(tconfig)) {
			throw new ServiceException("查询的系统配置[" + key + "]不存在！");
		}
		return po2vo(tconfig);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.basic.services.ISystemConfigManageService#checkExist(java.lang.String)
	 */
	@Override
	public boolean checkExist(String key) throws ServiceException {
		return systemConfigManageDao.checkExist(key);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.basic.services.ISystemConfigService#addSystemConfig(com
	 *      .eelpo.cmmp.basic.services.vo.SystemConfig)
	 */
	@Override
	public String addSystemConfig(SystemConfig config) throws ServiceException {

		// 检查系统配置是否已经存在
		if (systemConfigManageDao.checkExist(config.getConfigKey())) {
			throw new ServiceException("该系统配置已经存在！");
		}

		return systemConfigManageDao.addSystemConfig(vo2po(config));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.basic.services.ISystemConfigService#updateSystemConfig
	 *      (com.eelpo.cmmp.basic.services.vo.SystemConfig)
	 */
	@Override
	public void updateSystemConfig(SystemConfig config) throws ServiceException {

		// 检查系统配置是否已经存在
		if (systemConfigManageDao.checkExistByUpdate(config.getId(), config.getConfigKey())) {
			throw new ServiceException("该系统配置键值已经存在！");
		}

		systemConfigManageDao.updateSystemConfig(vo2po(config));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.basic.services.ISystemConfigService#deleteSystemConfig
	 *      (com.eelpo.cmmp.basic.services.vo.SystemConfig)
	 */
	@Override
	public void deleteSystemConfig(SystemConfig config) throws ServiceException {
		systemConfigManageDao.deleteSystemConfig(config.getId());
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.basic.services.ISystemConfigService#getSystemConfigsPaing
	 *      (com.eelpo.cmmp.basic.services.vo.SystemConfig, int, int)
	 */
	@Override
	public List<SystemConfig> getSystemConfigsPaing(SystemConfig config, int first, int max) throws ServiceException {
		return po2vo(systemConfigManageDao.getSystemConfigsPaing(config, first, max));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.basic.services.ISystemConfigService#getSystemConfigsCount
	 *      (com.eelpo.cmmp.basic.services.vo.SystemConfig)
	 */
	@Override
	public int getSystemConfigsCount(SystemConfig config) throws ServiceException {
		return systemConfigManageDao.getSystemConfigsCount(config);
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param config
	 *            系统配置值对象
	 * @return 返回系统配置实体对象
	 */
	public static TSystemConfig vo2po(SystemConfig config) {
		TSystemConfig tconfig = new TSystemConfig();
		BeanUtils.copyProperties(config, tconfig);
		return tconfig;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param tconfig
	 *            系统配置实体对象
	 * @return 返回系统配置值对象
	 */
	public static SystemConfig po2vo(TSystemConfig tconfig) {
		SystemConfig config = new SystemConfig();
		BeanUtils.copyProperties(tconfig, config);
		return config;
	}

	/**
	 * 静态转换，不包含任何关联对象的转换
	 * 
	 * @param tconfigs
	 *            系统配置实体对象列表
	 * @return 返回系统配置值对象列表
	 */
	public static List<SystemConfig> po2vo(List<TSystemConfig> tconfigs) {
		List<SystemConfig> configs = new ArrayList<SystemConfig>();
		for (TSystemConfig tconfig : tconfigs) {
			configs.add(po2vo(tconfig));
		}
		return configs;
	}

}
